﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Linq;

namespace VocabManager.DataLayer
{
    public class DeckRepository
    {
        FlashCardContext _dataContext;
        public DeckRepository(FlashCardContext dataContext)
        {
            _dataContext = dataContext;
        }
        public List<VocabManager.Model.Deck> GetDeckList()
        {
            return _dataContext.Decks.
                Select(item => new VocabManager.Model.Deck() { Id = item.Id,
                Name = item.Name,
                Description = item.Description,
                CreationTime = item.CreationTime
                }).ToList();
        }
        internal void RemoveDeck(VocabManager.Model.Deck deck)
        {
            Deck dataDeck = _dataContext.Decks.FirstOrDefault(item => item.Id == deck.Id);
           DatabaseManager.Insatance.DataContext.Decks.DeleteOnSubmit(dataDeck);
           DatabaseManager.Insatance.DataContext.Cards.DeleteAllOnSubmit(dataDeck.Cards);
           DatabaseManager.Insatance.DataContext.SubmitChanges();
            
        }

        internal void AddNewDeck(VocabManager.Model.Deck deck)
        {
            Deck dataDeck = new Deck { CreationTime = DateTime.Now, Description = deck.Description, Name = deck.Name };
            _dataContext.Decks.InsertOnSubmit(dataDeck);
            _dataContext.SubmitChanges();
            deck.Id = dataDeck.Id;
            deck.CreationTime = dataDeck.CreationTime;
            
        }

        internal void UpdateDeck(VocabManager.Model.Deck deck)
        {
           Deck dataDeck = _dataContext.Decks.FirstOrDefault(item => item.Id == deck.Id);
           AssignDeckToDataDeck(dataDeck, deck);
           _dataContext.SubmitChanges();

        }

        private void AssignDeckToDataDeck(Deck dataDeck, Model.Deck deck)
        {
            dataDeck.Name = deck.Name;
            dataDeck.Description = deck.Description;
        }
    }
}
